package com.example.clock;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.navigation.NavigationBarView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

 public class MainActivity extends AppCompatActivity {

     private String plan[] = new String[]{"计划1","计划2","计划3"};
     private String primary[] = new String[]{"运动","休闲","学习"};
     private int primaryindex;
     private String second[][] ={{"乒乓","体操","羽毛球"},{"音乐","短视频"},{"英语","理财","编程","美声"}};
     ArrayAdapter<String> adapter1,adapter2,adapter3;
     private Spinner spinner1, spinner2,spinner3;
     private Button btnSubmit;
     private Switch switchbutton;
     private TextView textView1,textView2;


     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addItemsOnSpinner1();  //设置锁屏时长下拉列表
        addItemsOnSpinner2();  //设置锁屏一级事件下拉列表
        addItemsOnSpinner3();  //设置锁屏二级事件下拉列表

        //侦听锁屏一级事件，根据一级事件选择结果确定二级事件下拉框内容
         spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent,View view, int position, long id)
            {
                primaryindex = position;
                adapter2 = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,second[position]);
                spinner3.setAdapter(adapter2);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }

        });

        addListenerOnButton(); //侦听界面“开始锁屏”按钮


    }

     public void addItemsOnSpinner1() {

         spinner1 = (Spinner) findViewById(R.id.spinner3);
         List<String> list = new ArrayList<String>();
         list.add("60");
         list.add("30");
         list.add("5");
         list.add("2");
         list.add("1");
         ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                 android.R.layout.simple_spinner_item, list);
         dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         spinner1.setAdapter(dataAdapter);
     }
     public void addItemsOnSpinner2() {

         spinner2 = (Spinner) findViewById(R.id.spinner5);

         adapter1 = new ArrayAdapter<String>(this,
                 android.R.layout.simple_spinner_item, primary);
         adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         spinner2.setAdapter(adapter1);
     }

     public void addItemsOnSpinner3() {

         spinner3 = (Spinner) findViewById(R.id.spinner6);
         adapter2 = new ArrayAdapter<String>(this,
                 android.R.layout.simple_spinner_item, second[0]);

         adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         spinner3.setAdapter(adapter2);
     }



     //收集界面输入数据，通过Serializable方式发送给锁屏界面，并开启锁屏界面
     public void lockstart(View view){

         spinner1 = (Spinner) findViewById(R.id.spinner3);
         spinner2 = (Spinner) findViewById(R.id.spinner5);
         spinner3 = (Spinner) findViewById(R.id.spinner6);


         //获取锁屏类型参数
         switchbutton = (Switch) findViewById(R.id.switch3);

         //获取锁屏备注参数
         textView1 = findViewById(R.id.editTextTextMultiLine);

         //获取锁屏任务名
         textView2 = findViewById(R.id.textView18);

         Transport transport = new Transport();
         transport.setPc(String.valueOf(spinner2.getSelectedItem()));//获取一级事件
         transport.setSc(String.valueOf(spinner3.getSelectedItem()));//获取二级事件

         transport.setType(switchbutton.isChecked()); //获取锁屏类型参数值
         transport.setDuration(String.valueOf(spinner1.getSelectedItem()));//获取锁屏时长

         String text1 = textView1.getText().toString(); //获取锁屏备注参数字符串
         transport.setEventnote(text1);
         String text2 = textView2.getText().toString(); //获取锁屏任务名
         transport.setTaskName(text2);


         Intent intent=new Intent(getApplicationContext(),LockActivity.class);//设置锁屏界面Intent
         intent.putExtra("Serializable", (Serializable) transport);//设置Serializable传递数据
         startActivity(intent);//开启锁屏界面

     }

     //侦听“开始锁屏按钮，发现点击后调用lockstart
     public void addListenerOnButton() {

         btnSubmit = (Button) findViewById(R.id.button);

         btnSubmit.setOnClickListener(new View.OnClickListener() {

             @Override
             public void onClick(View v) {

                    lockstart(v);

             }

         });
     }

 }
